package com.cc.jdkjucdemo.optional;

//import com.cc.jdkjucdemo.optional.entity.User;
import com.cc.jdkjucdemo.optional.entity.User;
import com.cc.jdkjucdemo.optional.repository.UserRepository;

import java.util.Optional;

public class Test2_01 {
    public static void main(String[] args) {
        // 0
//        User user = null;
//        if (user != null) {
//            System.out.println(user.getFullName());
//        } else {
//            user = new User("Stark", "Tony Stark");
//            System.out.println(user.getFullName());
//        }
        // 1
//        Optional<Object> empty = Optional.empty();
//        System.out.println(empty.isPresent());
//        System.out.println(empty.isEmpty());
        // 2
//        Optional<Object> optionalBox = Optional.empty();
//        String value1 = "Peter";
//        Optional<String> optionalBox1 = Optional.of(value1);
//        String value2 = "Peter";
//        Optional<String> optionalBox2 = Optional.ofNullable(value2);
        // 3
//        Optional<Object> optionalBox = Optional.empty();
//        optionalBox.get();

//        UserRepository userRepository = new UserRepository();
//        Optional<User> optionalUser = userRepository.findUserByName("Peter");
//        User user = optionalUser.orElse(
//                new User("Stark", "Tony Stark")
//        );
//        System.out.println(user.getFullName());

//        UserRepository userRepository = new UserRepository();
//        Optional<User> optionalUser = userRepository.findUserByName("Peter");
//        User user = optionalUser.orElseGet(
//                () -> new User("Stark", "Tony Stark")
//        );
//        System.out.println(user.getFullName());

//        UserRepository userRepository = new UserRepository();
//        Optional<User> optionalUser = userRepository.findUserByName("Peter2");
//        User user = optionalUser.orElseThrow(
//                () -> new RuntimeException("User not found")
//        );
//        System.out.println(user.getFullName());

          // 4
//        UserRepository userRepository = new UserRepository();
//        Optional<User> optionalUser = userRepository.findUserByName("Peter");
//        optionalUser.ifPresent(user -> System.out.println(user.getFullName()));

//        UserRepository userRepository = new UserRepository();
//        Optional<User> optionalUser = userRepository.findUserByName("Peter2");
//        optionalUser.ifPresentOrElse(user -> System.out.println(user.getFullName()), () -> System.out.println("User not found"));

//        UserRepository userRepository = new UserRepository();
//        Optional<User> optionalUser = userRepository.findUserByName("Peter");
//        Optional<User> op = optionalUser.filter(user -> user.getFullName().equals("Peter Parker"));
//        System.out.println(op.isPresent());

        // 5
//        UserRepository userRepository = new UserRepository();
//        Optional<User> optionalUser = userRepository.findUserByName("Peter");
//        Optional<String> fullName = optionalUser.map(User::getFullName);
//        System.out.println(fullName.get());

        UserRepository userRepository = new UserRepository();
        Optional<User> optionalUser = userRepository.findUserByName("Peter2");
        Optional<String> fullName = optionalUser.flatMap(User::getFullName2);
        System.out.println(fullName.get());
    }
}
